package com.lanou.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

/**
 * Created by dllo on 15/10/31.
 */
public class TimeActivity extends Activity implements View.OnClickListener {
    private TextView systemtime;

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
//				data_tv.setText(getData());
                    systemtime.setText(getData2());
                    break;

                default:
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);

        systemtime = (TextView) findViewById(R.id.systemtime);


        new Thread(){

            @Override
            public void run() {
                super.run();
                while(true){
                    handler.sendEmptyMessage(1);
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

        }.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.systemtime:
                break;

            default:
                break;
        }
    }

    /**
     * ��ȡ��ʱ�䣬���أ��磺22:00
     * @return
     */
//	public String getData(){
//		long time=System.currentTimeMillis();
//		final Calendar mCalendar=Calendar.getInstance();
//		mCalendar.setTimeInMillis(time);
//		//		 ȡ��Сʱ��
//		int mHour=mCalendar.get(Calendar.HOUR);
//		//		 ȡ�÷��ӣ�
//		int mMinuts=mCalendar.get(Calendar.MINUTE);
//		String mMinString = null;
//		if (mMinuts<10){
//			mMinString = "0"+mMinuts;
//		}else{
//			mMinString = ""+mMinuts;
//		}
//		return mHour+":"+mMinString;
//	}

    public String getData2(){
        Time time = new Time("GMT+8");
        time.setToNow();
        int year = time.year;
        int month = time.month;
        int day = time.monthDay;
        int minute = time.minute;
        int hour = time.hour+8;
        int sec = time.second;
        String mMinString = null;
        String secString = null;
        if (minute<10){
            mMinString = "0"+minute;
        }else{
            mMinString = ""+minute;
        }

        if (sec<10){
            secString = "0"+sec;
        }else{
            secString = ""+sec;
        }

        String str1 = "time:" + year +
                "." + month +
                "." + day +
                "." + hour +
                ":" + mMinString +
                ": " + secString;

        return str1;
    }

}
